package com.ilibrary.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.ilibrary.base.MyBaseAction;
import com.ilibrary.constants.MyConstants;
import com.ilibrary.service.BookService;
import com.ilibrary.utils.MyTools;
import com.ilibrary.web.dto.ResultDto;
import com.ilibrary.web.vo.BookVO;

public class DoSearchBooksAction extends MyBaseAction {
	
	private BookService bookService;
	
	public BookService getBookService() {
		return bookService;
	}
	public void setBookService(BookService bookService) {
		this.bookService = bookService;
	}
	
	@Override
	public String execute() throws Exception {
		setCharacterEncoding();
		HttpServletRequest request = getRQ();
		String keywords = MyTools.convert2zHString(request.getParameter("keywords"));
		int currentPage = Integer.parseInt(request.getParameter("currentPage"));
		logger.info(keywords);
		logger.info("currentPage:"+currentPage);
		int start = (currentPage-1)*MyConstants.CLIENT_SEARCH_BOOK_PAGE_SIZE;
		int limit = MyConstants.CLIENT_SEARCH_BOOK_PAGE_SIZE;
		ResultDto<BookVO> resultDto = bookService.queryListByKeyword(start, limit, keywords);
		List<BookVO> bookVOList = resultDto.getResultList();
		int total = resultDto.getTotal();
		int totalPages = MyTools.convertItemCnt2PageCnt(total, MyConstants.CLIENT_SEARCH_BOOK_PAGE_SIZE);
		request.setAttribute("bookVOList", bookVOList);
		request.setAttribute("keywords", keywords);
		request.setAttribute("totalPages", totalPages);
		request.setAttribute("currentPage", currentPage);
		return SUCCESS;
	}
	
}
